package com.hz.userservice.service.impl;

import com.hz.apiservice.model.dto.StudentDTO;
import com.hz.userservice.mapper.ClassMapper;
import com.hz.userservice.mapper.GradeMapper;
import com.hz.userservice.mapper.UserMapper;
import com.hz.userservice.model.entity.ClassEntity;
import com.hz.userservice.model.entity.GradeEntity;
import com.hz.userservice.model.entity.UserEntity;
import com.hz.userservice.service.StudentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    UserMapper userMapper;
    @Resource
    GradeMapper gradeMapper;
    @Resource
    ClassMapper classMapper;
    @Override
    public StudentDTO getStudentById(Integer id) {
        UserEntity userEntity=userMapper.findStudentById(id);
        if (userEntity==null){
            return null;
        }
        StudentDTO student=new StudentDTO();
        student.setId(userEntity.getId());
        student.setUsername(userEntity.getUsername());
        student.setRealname(userEntity.getRealname());
        student.setStudentId(userEntity.getStudent_id());
        student.setGradeId(userEntity.getGrade_id());
        GradeEntity gradeEntity=gradeMapper.findByGradeId(userEntity.getGrade_id());
        student.setGradeName(gradeEntity.getGrade_name());
        student.setGradeYear(gradeEntity.getGrade_year());
        ClassEntity classEntity=classMapper.findByClassId(userEntity.getClass_id());
        student.setClassId(classEntity.getClass_id());
        student.setClassName(classEntity.getClass_name());
        return student;
    }
}
